package com.wxapi.model

import com.module.frame.base.BaseFrameViewModel
import com.module.frame.ext.postEvent
import com.module.third.service.http.getUserInfoService
import com.module.third.wx.bean.WxToken
import com.module.third.wx.bean.WxUserInfo
import com.module.third.ext.request2
import kotlinx.coroutines.flow.MutableSharedFlow

class WXEntryViewModel : BaseFrameViewModel() {

    /**
     * token成功
     */
    var tokenSucFlow: MutableSharedFlow<WxToken> = MutableSharedFlow()

    /**
     * 用户信息成功
     */
    var infoSucFlow: MutableSharedFlow<WxUserInfo> = MutableSharedFlow()

    /**
     * 失败
     */
    var failFlow: MutableSharedFlow<String> = MutableSharedFlow()

    fun getWxToken(appid: String, secret: String, code: String, grant_type: String) {
        request2({
            getUserInfoService().getWxToken(appid, secret, code, grant_type)
        }, {
            if (it != null) {
                postEvent(tokenSucFlow, it)
            } else {
                postEvent(failFlow, "貌似出了点差错，请稍后重试~")
            }
        }, {
            postEvent(failFlow, "貌似出了点差错，请稍后重试~")
        })
    }

    fun getWxInfo(access_token: String?, openid: String?) {
        request2({
            getUserInfoService().getWxInfo(access_token, openid)
        }, {
            if (it != null) {
                postEvent(infoSucFlow, it)
            } else {
                postEvent(failFlow, "貌似出了点差错，请稍后重试~")
            }
        }, {
            postEvent(failFlow, "貌似出了点差错，请稍后重试~")
        })
    }
}